<?php /* COMPANIES $Id: vw_depts.php,v 1.15.2.1 2006/07/31 11:54:15 nybod Exp $ */
##
##	Companies: View Projects sub-table
##
global $AppUI, $company_id, $canEdit;

$q  = new DBQuery;
$q->addTable('companies');
$q->addQuery('companies.*');
$q->addWhere('company_parent = '.$company_id);

$sql = $q->prepare();
$q->clear();

// function renamed to avoid naming clash
function showbranch( &$a, $level=0 ) {
	global $AppUI;
	$s = '
	<td>
		<a href="index.php?m=companies&a=addedit&company_id='.$a['company_id'].'" title="'.$AppUI->_('edit').'">
			' . dPshowImage( 'images/edit.gif', 12, 12, '' ) . '
	</td>
	<td>';

	for ($y=0; $y < $level; $y++)
	{
		if ($y+1 == $level)
			$s .= '<img src="images/corner-dots.gif" width="16" height="12" border="0">';
		else
			$s .= '<img src="images/shim.gif" width="16" height="12" border="0">';
	}

	$s .= '<a href="index.php?m=companies&a=view&company_id='.$a['company_id'].'">'.$a['company_name'].'</a>';
	$s .= '</td>';
	//$s .= '<td align="center">'.($a['dept_users'] ? $a['dept_users'] : '').'</td>';

	echo '<tr class="second">'.$s.'</tr>';
}

// function renamed to avoid naming clash
function findchildbranch($parent_id, $level=0 ){
	$level = $level+1;
	$qb  = new DBQuery;
	$qb->addTable('companies');
	$qb->addQuery('companies.*');
	$qb->addWhere('company_parent = '.$parent_id);
	$sqlb = $qb->prepare();
	$childs = db_loadList( $sqlb, NULL );
	if(count($childs)){
		foreach ($childs as $child) {
			showbranch($child , $level);
			findchildbranch($child['company_id']);
		}
	}
}

?>
<table border="0" cellpadding="7" cellspacing="0" width="100%" class="infopanel">
<tr>
	<td>
<? if ($canEdit) {
	showButton($AppUI->_( 'new branch' ), 'index.php?m=companies&a=addedit&company_id=0&company_parent='.$company_id);
} ?>
	</td>
</tr>
</table>
<table width="100%" border="0" cellpadding="2" cellspacing="0" class="list">
<tr><th>&nbsp;</th><th width="100%"><?=$AppUI->_( 'Name' )?></th></tr>
<tr class="second">
<?
$rows = db_loadList( $sql, NULL );
if (!count( $rows)) {
	echo '<td colspan="2">' . $AppUI->_('No data available') . '</td>';
}
?>
</tr>
<?
foreach ($rows as $row) {
	showbranch( $row );
	findchildbranch($row['company_id']);
}

?>
</table>
